<?php
/**
 * 文本投票
 * @author 回忆QQ125879930
 * @url
 */
defined('IN_IA') or exit('Access Denied');

class Huiyi_textvoteModuleProcessor extends WeModuleProcessor {

    public $table_reply='huiyi_textvote_reply';
    public $table_setting='huiyi_textvote_setting';
    public $table_option='huiyi_textvote_option';
    public $table_log='huiyi_textvote_log';

    public function respond() {
        global $_W;

        $rid = $this->rule;
        $msg = $this->message['content'];
        $openid = $this->message['from'];

        //获取投票信息
        $reply = $this->getReply($rid);
        $vote = $this->getVote($reply['vid']);
        $options = $this->getOptions($vote['id']);

        if(!$this->inContext) {
            $this->beginContext(600);

            //投票欢迎提示
            $content = "欢迎参与《".$vote['title']."》投票活动\r\n本次活动选项:\r\n";
            foreach($options as $k => $v){
                $content.=$v['code'].'. '.$v['title']."\r\n";
            }
            $content.="回复相应 “编号” 进行投票！\r\n";
            $content.="回复 “" . $reply['exit_key'] . "” 退出投票！";

        } else {
            //if ($msg == "退出投票") {
            if ($msg == $reply['exit_key']) {
                $this->endContext();
                $content = '谢谢您的参与~';
            } else {
                if ($status['http_code'] = 200) {
                    //投票检测是否符合规则
                    if (intval($msg)) {
                        $oid = intval($msg);
                        //1.检测能否投票
                        //2.记录投票数据
                        $result = $this -> option_addone($oid, $openid);
                        if ($result) {
                            $content = "投票成功谢谢参与\r\n回复“编号”继续投票\r\n回复 “" . $reply['exit_key'] . "” 退出投票！";
                        } else {
                            $content = "编号好像错了呦，请输入正确的编号参与投票~";
                        }
                    } else {
                        $content = "请输入编号，进行投票~\r\n或者回复 “" . $reply['exit_key'] . "” 退出投票！";
                    }
                } else {
                    $content = '那个...小i不知道怎么回答您，要不您发送[退出]再发：[梦梦]和梦梦姐聊聊？';
                }
            }
        }
        return $this->respText($content);

    }

    public function getReply($rid) {
        $reply = pdo_fetch("SELECT * FROM ".tablename($this->table_reply)." WHERE rid = :rid", array(':rid' => $rid));
        return $reply;
    }

    public function getVote($id) {
        $vote = pdo_fetch("SELECT * FROM ".tablename($this->table_setting)." WHERE id = :id", array(':id' => $id));
        return $vote;
    }

    public function getOptions($vid) {
        $options = pdo_fetchall("SELECT * FROM ".tablename($this->table_option)." WHERE vid = :vid", array(':vid' => $vid));
        return $options;
    }
    public function option_addone($oid, $openid) {
        $option = pdo_fetch("SELECT * FROM ".tablename($this->table_option)." WHERE id = :id", array(':id' => $oid));
        if (empty($option)) {
            return false;
        } else {
            //1.选项票数+1
            $data = array(
                'total' => $option['total'] + 1,
            );
            pdo_update($this->table_option, $data, array('id' => $oid));

            //2.投票记录+1
            $data = array(
                'from_user' => $openid,
                'state' => 0,
                'create_time' => TIMESTAMP,
                'oid' => $oid,
                'option_title' => $option['title'],
                'vid' => $option['vid'],
            );
            pdo_insert($this -> table_log, $data);

            return true;
        }
    }


}